package com.cos.listener;

import com.cos.constant.Sms;
import com.cos.utils.HttpUtils;

import com.cos.vo.SmsVo;
import com.rabbitmq.client.Channel;
import lombok.Data;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@ConfigurationProperties(prefix = "sms")
@Data
public class SmsListener {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    //接口的地址
    private String host ;
    //接口的路径
    String path ;

    //我自己的appcode
    String appcode;
    @RabbitListener(queues = Sms.SMS_QUEUE)
    public void sms(SmsVo smsVo, Channel channel, Message message){
        //接口的请求方式
        String method ="POST";
        System.out.println("发送短信,内容是:"+smsVo);
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", smsVo.getPhone());
        querys.put("templateId", smsVo.getTemplated());
        querys.put("value", smsVo.getCode());
        Map<String, String> bodys = new HashMap<String, String>();

        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
//            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
//            System.out.println(response.toString());
//            //获取response的body
//            System.out.println(EntityUtils.toString(response.getEntity()));
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
